package com.bond.jumper.client.netty;

import com.bond.jumper.client.ScreenInfo;
import com.bond.jumper.client.screengather.ClientWork;
import com.bond.jumper.common.po.MessageToClient;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.awt.event.InputEvent;

public class NettyClientHandler extends SimpleChannelInboundHandler<MessageToClient> {

    public NettyClientHandler() {
        super();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端建立连接");
        ClientWork.startWork();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof MessageToClient) {
            MessageToClient messageToClient = (MessageToClient) msg;
            //System.out.println("to client -> " + messageToClient);
            //处理鼠标位置
            ScreenInfo.instance.getRobot().mouseMove(messageToClient.getX(), messageToClient.getY());
            //模拟鼠标点击
            if (messageToClient.getMouseKey() == 0) {
                ScreenInfo.instance.getRobot().mousePress(InputEvent.BUTTON1_DOWN_MASK);
                //释放鼠标按键，如果不是放就是长按
                ScreenInfo.instance.getRobot().mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            } else if (messageToClient.getMouseKey() == 1) {
                ScreenInfo.instance.getRobot().mousePress(InputEvent.BUTTON2_DOWN_MASK);
                ScreenInfo.instance.getRobot().mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
            } else if (messageToClient.getMouseKey() == 2) {
                ScreenInfo.instance.getRobot().mousePress(InputEvent.BUTTON3_DOWN_MASK);
                ScreenInfo.instance.getRobot().mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
            }
            //模拟鼠标滚动
            if (messageToClient.getMouseWheel() != 0) {
                ScreenInfo.instance.getRobot().mouseWheel(messageToClient.getMouseWheel());
            }
            //模拟键盘输入
            if (messageToClient.getKeyCode() != 0) {
                try {
                    //macos 特殊按键映射：<>?:"[]{}\|

                    ScreenInfo.instance.getRobot().keyPress(messageToClient.getKeyCode());
                } catch (Exception e) {
                    // TODO 待解决Macos部分按键无法映射到windows
                    //java.lang.IllegalArgumentException: Invalid key code
                    System.out.println("Invalid key code :" + messageToClient.getKeyCode() + "," + messageToClient.getKey());
                }
            }
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageToClient messageToClient) throws Exception {

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端断开连接");
        ClientWork.stopWork();
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        super.channelWritabilityChanged(ctx);
    }


}